from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt
import matplotlib,csv
reader=csv.reader(open("../Wine Reviews/dataset/winemag-data_first150k.csv","r",encoding="utf-8"))
matplotlib.rcParams['font.sans-serif']=['SimHei']   # 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False     # 正常显示负号
reader=csv.reader(open("../Wine Reviews/dataset/winemag-data_first150k.csv","r",encoding="utf-8"))
prices,points=[],[]
for row in reader:
    points.append(row[4])  # 索引表示所在列
    if len(row[5]) > 0:prices.append(row[5])
    else: prices.append(-1)
points=[float(points[i]) for i in range(1,len(points))]
prices=[float(prices[i]) for i in range(1,len(prices))]
x,y=[],[]
for i in range(len(points)):
    if prices[i]!=-1:
        x.append([points[i]])
        y.append(prices[i])
reg=LinearRegression().fit(x,y)
lost,lostIndex=[],[]
for i in range(len(points)):
    if prices[i]==-1:
        lost.append([points[i]])
        lostIndex.append(i)
predictList=list(reg.predict(lost))
for i,index in enumerate(lostIndex):
    prices[index]=predictList[i]
plt.ticklabel_format(style="plain")
plt.title("价格price盒图")
plt.boxplot(prices,showmeans=True,showfliers=False)
plt.show()